#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;
//define small top heap
priority_queue<int, vector<int>, greater<int>> pri_queue;

int main(int argc, char const *argv[]){
    int n;
    while (scanf("%d", &n) != EOF) {
        while (!pri_queue.empty())
            //clear the small top heap
            pri_queue.pop();
        for (int i = 0; i < n; ++i) {
            int weight;
            scanf("%d", &weight);
            pri_queue.push(weight);
        }
        int result = 0;
        while (pri_queue.size() > 1){
            int weight_1 = pri_queue.top();
            pri_queue.pop();
            int weight_2 = pri_queue.top();
            pri_queue.pop();
            result += weight_1 + weight_2;
            pri_queue.push(weight_1 + weight_2);
        }
        printf("%d\n", result);
    }
    return 0;
}